package cn.toma.core.web;

import cn.toma.common.converter.EnumConvertFactory;
import cn.toma.core.web.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author zqx
 * @version 1.0.0
 * @ClassName WebConfig.java
 * @Description Web mvc 配置Bean
 * @createTime 2019年08月28日 16:39:00
 */
@Configuration
public class WebConfig implements WebMvcConfigurer
{

	/**
	 * 注册枚举类型转换器注册工厂
	 *
	 * @param registry
	 */
	@Override
	public void addFormatters(FormatterRegistry registry)
	{
		registry.addConverterFactory(new EnumConvertFactory());
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry)
	{
		registry.addInterceptor(myAuthenticationInterceptor());
	}

	/**
	 * 自定义拦截器 需要注意 这里需要先实例化一个 bean 而不是直接在 addInterceptors中新new一个实例
	 * 这样会导致原有实例不能注入其他的组件 因为会有2个不同的实例
	 *
	 * @return
	 */
	@Bean
	public AuthenticationInterceptor myAuthenticationInterceptor()
	{
		return new AuthenticationInterceptor();
	}




	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry)
	{
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
}
